home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / GNUSUTIL.ZIP / src / uname.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-17  |  4.6 KB  |  208 lines

  1. /* uname -- print system information
  2.    Copyright (C) 89, 90, 91, 92, 93, 1994 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Option        Example
  19.  
  20.    -s, --sysname    SunOS
  21.    -n, --nodename    rocky8
  22.    -r, --release    4.0
  23.    -v, --version    
  24.    -m, --machine    sun
  25.    -a, --all        SunOS rocky8 4.0  sun
  26.  
  27.    The default behavior is equivalent to `-s'.
  28.  
  29.    David MacKenzie <djm@gnu.ai.mit.edu> */
  30.  
  31. #include <config.h>
  32. #include <stdio.h>
  33. #include <sys/types.h>
  34. #ifdef OS2
  35. #include <utsname.h>
  36. #else
  37. #include <sys/utsname.h>
  38. #endif
  39. #include <getopt.h>
  40.  
  41. #include "system.h"
  42. #include "version.h"
  43.  
  44. void error ();
  45.  
  46. static void print_element ();
  47. static void usage ();
  48.  
  49. /* Values that are bitwise or'd into `toprint'. */
  50. /* Operating system name. */
  51. #define PRINT_SYSNAME 1
  52.  
  53. /* Node name on a communications network. */
  54. #define PRINT_NODENAME 2
  55.  
  56. /* Operating system release. */
  57. #define PRINT_RELEASE 4
  58.  
  59. /* Operating system version. */
  60. #define PRINT_VERSION 8
  61.  
  62. /* Machine hardware name. */
  63. #define PRINT_MACHINE 16
  64.  
  65. /* Mask indicating which elements of the name to print. */
  66. static unsigned char toprint;
  67.  
  68. /* The name this program was run with, for error messages. */
  69. char *program_name;
  70.  
  71. /* If non-zero, display usage information and exit.  */
  72. static int show_help;
  73.  
  74. /* If non-zero, print the version on standard output and exit.  */
  75. static int show_version;
  76.  
  77. static struct option const long_options[] =
  78. {
  79.   {"help", no_argument, &show_help, 1},
  80.   {"machine", no_argument, NULL, 'm'},
  81.   {"nodename", no_argument, NULL, 'n'},
  82.   {"release", no_argument, NULL, 'r'},
  83.   {"sysname", no_argument, NULL, 's'},
  84.   {"version", no_argument, &show_version, 1},
  85.   {"all", no_argument, NULL, 'a'},
  86.   {NULL, 0, NULL, 0}
  87. };
  88.  
  89. void
  90. main (argc, argv)
  91.      int argc;
  92.      char **argv;
  93. {
  94.   struct utsname name;
  95.   int c;
  96.  
  97.   program_name = argv[0];
  98.   toprint = 0;
  99.  
  100.   while ((c = getopt_long (argc, argv, "snrvma?", long_options, (int *) 0))
  101.      != EOF)
  102.     {
  103.       switch (c)
  104.     {
  105.     case 0:
  106.       break;
  107.  
  108.     case 's':
  109.       toprint |= PRINT_SYSNAME;
  110.       break;
  111.  
  112.     case 'n':
  113.       toprint |= PRINT_NODENAME;
  114.       break;
  115.  
  116.     case 'r':
  117.       toprint |= PRINT_RELEASE;
  118.       break;
  119.  
  120.     case 'v':
  121.       toprint |= PRINT_VERSION;
  122.       break;
  123.  
  124.     case 'm':
  125.       toprint |= PRINT_MACHINE;
  126.       break;
  127.  
  128.     case 'a':
  129.       toprint = PRINT_SYSNAME | PRINT_NODENAME | PRINT_RELEASE |
  130.         PRINT_VERSION | PRINT_MACHINE;
  131.       break;
  132.  
  133.     default:
  134.       usage (0);
  135.     }
  136.     }
  137.  
  138.   if (show_version)
  139.     {
  140.       printf ("uname - %s\n", version_string);
  141.       exit (0);
  142.     }
  143.  
  144.   if (show_help)
  145.     usage (0);
  146.  
  147.   if (optind != argc)
  148.     usage (1);
  149.  
  150.   if (toprint == 0)
  151.     toprint = PRINT_SYSNAME;
  152.  
  153.   if (uname (&name) == -1)
  154.     error (1, errno, "cannot get system name");
  155.  
  156.   print_element (PRINT_SYSNAME, name.sysname);
  157.   print_element (PRINT_NODENAME, name.nodename);
  158.   print_element (PRINT_RELEASE, name.release);
  159.   print_element (PRINT_VERSION, name.version);
  160.   print_element (PRINT_MACHINE, name.machine);
  161.  
  162.   exit (0);
  163. }
  164.  
  165. /* If the name element set in MASK is selected for printing in `toprint',
  166.    print ELEMENT; then print a space unless it is the last element to
  167.    be printed, in which case print a newline. */
  168.  
  169. static void
  170. print_element (mask, element)
  171.      unsigned char mask;
  172.      char *element;
  173. {
  174.   if (toprint & mask)
  175.     {
  176.       toprint &= ~mask;
  177.       printf ("%s%c", element, toprint ? ' ' : '\n');
  178.     }
  179. }
  180.  
  181. static void
  182. usage (status)
  183.      int status;
  184. {
  185.   if (status != 0)
  186.     fprintf (stderr, "Try `%s --help' for more information.\n",
  187.          program_name);
  188.   else
  189.     {
  190.       print_version("uname");
  191.       printf ("Usage: %s [OPTION]...\n", program_name);
  192.       printf ("\
  193. \n\
  194.   -a, --all        print all information\n\
  195.   -m, --machine    print the machine (hardware) type\n\
  196.   -n, --nodename   print the machine's network node hostname\n\
  197.   -r, --release    print the operating system release\n\
  198.   -s, --sysname    print the operating system name\n\
  199.   -v               print the operating system version\n\
  200.       --help       display this help and exit\n\
  201.       --version    output version information and exit\n\
  202. \n\
  203. Without any OPTION, assume -s.\n\
  204. ");
  205.     }
  206.   exit (status);
  207. }
  208.